/*
 * @Descripttion: 文件说明
 * @version: 0.0.1
 * @Author: gaojiapeng
 * @Date: 2021-02-18 10:34:14
 * @LastEditors: gaojiapeng
 * @LastEditTime: 2021-03-08 21:22:28
 */

import { Chart, registerShape, Util } from "@antv/g2";
function newChart(option) {
  option.dom.innerHTML = "";
  var chart = new Chart({
    container: option.dom,
    autoFit: true,
    padding: [option.padding ? option.padding : 20]
  });
  if (option.triangle === true) {
    registerShape("interval", "triangle", {
      // 1. 定义关键点
      getPoints(cfg) {
        const x = cfg.x;
        const y = cfg.y;
        const y0 = cfg.y0;
        const width = cfg.size;
        return [
          { x: x - width / 2, y: y0 },
          { x: x, y: y },
          { x: x + width / 2, y: y0 }
        ];
      },
      // 2. 绘制
      draw(cfg, group) {
        const points = this.parsePoints(cfg.points); // 将0-1空间的坐标转换为画布坐标
        const polygon = group.addShape("path", {
          attrs: {
            path: [
              ["M", points[0].x, points[0].y],
              ["L", points[1].x, points[1].y],
              ["L", points[2].x, points[2].y]
            ],
            ...cfg.style
          }
        });
        return polygon;
      }
    });
  }
  chart.registerShape = registerShape;
  chart.interaction("element-active");
  chart.Util = Util;
  chart.setAxisTitle = (
    view,
    config,
    style = {
      fill: "#8c8c8c",
      fontSize: 12,
      fontWeight: 300
    }
  ) => {
    view.annotation().text({
      ...config,
      style
    });
  };
  chart.interVal = data => {
    let count = 0;
    const maxCount = data.length;
    const newData = [];
    let interVal = setInterval(() => {
      if (count < maxCount) {
        newData.push(data[count]);
        chart.changeData(newData);
        console.log(newData);
        count++;
      } else {
        interVal = null;
      }
    }, 1000);
  };

  return chart;
}

export default newChart;
